<?php

/*
 * Niken Larasati
 * larasati.n@students.amikom.ac.id
 */

if (!defined('BASEPATH'))
    exit('Tidak ada script yang bisa diakses');

class login extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

//index menampilkan halaman login
    function index() {
        $this->load->view('login_v');
    }

//fungsi untuk proses login
    public function proses() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('user', 'username', 'required');
        $this->form_validation->set_rules('pass', 'password', 'required');
        if ($this->form_validation->run() == FALSE) {
            redirect('login/index');
        } else {
            $username = $this->input->post('user');
            $password = MD5($this->input->post('pass'));

            $this->load->model('pengguna_m');
            $cek_baris = $this->pengguna_m->cek_login($username, $password);
            
            if ($cek_baris->num_rows > 0) {
                $data_pengguna = $cek_baris->row();

//disini lakukan pemisahan hak akses
                $datalogin['username'] = $data_pengguna->username;
                $datalogin['password'] = $data_pengguna->password;
                $datalogin['level'] = $data_pengguna->level;
                $this->session->set_userdata('login', $datalogin);
                redirect('halaman');
                
            } else {
//menampilkan pesan validasi jika username/password salah
                $pesan['pesan'] = 'Username atau password salah!';
                $this->load->view('login_v',$pesan);                
            }
        }
    }

//akhir proses()
//fungsi untuk proses logout
    public function logout() {
        $this->session->sess_destroy();
        $this->pengguna_m->logout();
        redirect('proses');
    }

}
?>
